package com.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Map;
import org.apache.log4j.Logger;

import freemarker.core.Environment;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateException;
/**
 * 
 * <p>功能描述:用来生成代码文件</p>
 * @author  <a href="mailto: zhuyoulong@gmail.com">zhuyoulong</a>
 * @create_date 2008-11-4
 * @version $ Revision: 1.0
 */
public class FreeMarkerHelper {

	private Logger logger = Logger.getLogger(FreeMarkerHelper.class);
	
	private Configuration freemarker_cfg;
	private String templateLocal;
	public FreeMarkerHelper(){
		
	}
	/**
	 * 得到模板的配置
	 * @return
	 */
	public FreeMarkerHelper(String templateLocal){
		this.templateLocal = templateLocal;
	}
	private Configuration getConfiguration(){
		if(freemarker_cfg == null){
			freemarker_cfg = new Configuration();
			try {
				//设置模板文件存放的路径
				freemarker_cfg.setDirectoryForTemplateLoading(new File(templateLocal));
				freemarker_cfg.setObjectWrapper(new DefaultObjectWrapper());
			} catch (IOException e) {
				logger.debug(e);
			}
		}
		return freemarker_cfg;
	}
	/**
	 * 生成文件
	 * @param root 模板对应的数据模型
	 * @param template 模板文件名
	 * @param local 静态文件存放位置
	 * @param fileName 静态文件名
	 * @return
	 */
	public boolean createStaticPage(Map root,String template,String local,String fileName){
		boolean result = true;
		if(!createFile(local)){
			logger.debug("不能创建目录,文件创建失败");
			return false;
		}
		File staticFile = new File(local+fileName);
		Writer out = null;
		try {
			//设置输出的字符类型
			out =  new OutputStreamWriter(new FileOutputStream(staticFile),"utf-8");
			//Writer out =  new OutputStreamWriter(new FileOutputStream(staticFile));
			Template t = getConfiguration().getTemplate(template,ConfigUtil.getConfigPropertyValue("freemarker.template.encoding"));
			Environment e = t.createProcessingEnvironment(root,out);
			e.setOutputEncoding("utf-8");
			e.process();
			out.flush();
		} catch (FileNotFoundException e) {
			logger.error("模板打开失败.");
			logger.error(e);
			result = false;
		} catch (IOException e) {
			logger.error("文件创建失败");
			logger.error(e);
			result = false;
		} catch (TemplateException e) {
			logger.error(e);
			result = false;
		}finally{
			if(out !=null){
				try {
					out.close();
				} catch (IOException e) {
					logger.error(e);
				}
			}
		}
		return result;
	}
	/**
	 * 创建目录
	 * @param dir
	 * @return
	 */
	private boolean createFile(String dir){
		boolean result = true;
		File directory = new File(dir);
		if(!directory.exists()){			
			if(!directory.mkdirs()){
				logger.debug("文件路径创建失败");
				result = false;
			}
		}		
		return result;
	}
//	
//	public static void main(String[] args) {
//		FreeMarkerHelper ff = new FreeMarkerHelper("");
//		
//		try {
//			Template t = ff.getConfiguration().getTemplate("/com/action/ActionTemplate.ftl");
//			System.out.println(t.getName());
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//	}
}
